<?php


namespace app\finance\controller;


use app\base\BaseController;
use think\facade\Db;

class MoneyRecords extends BaseController
{
    protected $title='收支明细';
    protected $table='money_records';
    public function index()
    {
        $param=$this->request->param();
        if (request()->isAjax()) {
            $where[]=['mr.status','>',-1];
            $where[]=['mr.orderType','<>',6];

            if (isset($this->param['orderType'])&&$this->param['orderType']){
                $where[]=['mr.orderType','=',$this->param['orderType']];
            }
            if (isset($this->param['mType'])&&$this->param['mType']){
                $where[]=['mr.mType','=',$this->param['mType']];
            }
            if (isset($this->param['name'])&&$this->param['name']){
                $where[]=['py.name|py.code|ad.name|mr.relatedOrderNum','like','%'.$this->param['name'].'%'];
            }
            if (isset($this->param['orderDate'])&&$this->param['orderDate']){
                $dates=explode('~',$this->param['orderDate']);
                $where[]=['mr.orderDate','between',$dates];
            }
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $list = Db::name($this->table)
                ->alias('mr')
                ->leftJoin('purveyor py','py.id=mr.purveyorId')
                ->leftJoin('admin ad','ad.id=mr.uid')
                ->where($where)
                ->field('mr.*,py.name as pName,ad.name as userName')
                ->order('mr.id DESC')
                ->paginate($rows)->each(function ($item){
                    $item['puName']=$item['pName']?$item['pName']:$item['userName'];
                    return $item;
                });
            $countIn=Db::name($this->table)
                ->alias('mr')
                ->leftJoin('purveyor py','py.id=mr.purveyorId')
                ->leftJoin('admin ad','ad.id=mr.uid')
                ->where($where)
                ->where('mr.mType',1)
                ->sum('money');
            $countOut=Db::name($this->table)
                ->alias('mr')
                ->leftJoin('purveyor py','py.id=mr.purveyorId')
                ->leftJoin('admin ad','ad.id=mr.uid')
                ->where($where)
                ->where('mr.mType',2)
                ->sum('money');
            return table_assign(0, '操作成功', $list,$httpCode = 200, $header = [], $options = [],$others=['countIn'=>$countIn,'countOut'=>$countOut]);
        } else {
            return view();
        }
    }
}